package com.jm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jm.constants.SystemConstants;
import com.jm.domain.ResponseResult;
import com.jm.domain.dto.ChangeRoleStatusDto;
import com.jm.domain.dto.RoleDto;
import com.jm.domain.entity.Role;
import com.jm.domain.entity.RoleMenu;
import com.jm.domain.vo.PageVo;
import com.jm.domain.vo.RoleVo;
import com.jm.mapper.RoleMapper;
import com.jm.service.RoleMenuService;
import com.jm.service.RoleService;
import com.jm.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2023-06-24 21:23:20
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Override
    public List<String> selectRolesByUserId(Long id) {
        //判断是否是管理员，如果是集合中只有admin
        if (id == 1L) {
            List<String> roles = new ArrayList<>();
            roles.add("admin");
            return roles;
        }
        //否则查询用户所具有的角色信息
        return getBaseMapper().selectRolesByUserId(id);
    }

    @Override
    public ResponseResult listRole(Integer pageNum, Integer pageSize, String roleName, String status) {
        //查询条件
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(roleName != null && roleName != "", Role::getRoleName, roleName);
        queryWrapper.eq(status != null && status != "", Role::getStatus, status);
        queryWrapper.orderByAsc(Role::getRoleSort);
        //分页查询
        Page<Role> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);
        //封装vo
        List<Role> records = page.getRecords();
        List<RoleVo> roleVos = BeanCopyUtils.copyBeanList(records, RoleVo.class);
        PageVo pageVo = new PageVo(roleVos, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult changeStatus(ChangeRoleStatusDto changeRoleStatusDto) {
        //查询
        Role role = getBaseMapper().selectById(changeRoleStatusDto.getRoleId());
        //修改状态
        role.setStatus(changeRoleStatusDto.getStatus());
        //修改
        updateById(role);
        return ResponseResult.okResult();
    }

    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    @Transactional
    public ResponseResult saveRole(RoleDto roleDto) {
        Role role = BeanCopyUtils.copyBean(roleDto, Role.class);
        //保存role对象到角色表
        save(role);
        //获取menuIds，添加角色菜单关系表
        List<String> menuIds = roleDto.getMenuIds();
        List<RoleMenu> roleMenuList=menuIds.stream()
                .map(menuId->new RoleMenu(role.getId(),Long.valueOf(menuId)))
                .collect(Collectors.toList());
        roleMenuService.saveBatch(roleMenuList);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getRoleById(Long id) {
        //查询数据库，获取数据
        Role role = getBaseMapper().selectById(id);
        //封装vo
        RoleVo roleVo = BeanCopyUtils.copyBean(role, RoleVo.class);
        return ResponseResult.okResult(roleVo);
    }

    @Override
    @Transactional
    public ResponseResult updateRole(Role role) {
        //删除角色菜单表中相应的关系
        LambdaQueryWrapper<RoleMenu> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenu::getRoleId,role.getId());
        roleMenuService.remove(queryWrapper);
        //添加新的角色菜单关系
        List<RoleMenu> roleMenuList = role.getMenuIds().stream()
                .map(menuId -> new RoleMenu(role.getId(), Long.valueOf(menuId)))
                .collect(Collectors.toList());
        roleMenuService.saveBatch(roleMenuList);
        //更改role的信息
        updateById(role);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteRole(Long id) {
        //查询角色菜单关系表，删除相应的关系
        LambdaQueryWrapper<RoleMenu> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenu::getRoleId,id);
        roleMenuService.remove(queryWrapper);
        //删除角色
        removeById(id);
        return ResponseResult.okResult();
    }

    @Autowired
    private RoleService roleService;

    @Override
    public ResponseResult listAllRole() {
        //查寻role表，获取所有状态正常的数据
        LambdaQueryWrapper<Role> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getStatus, SystemConstants.STATUS_NORMAL);
        List<Role> roleList = roleService.list(queryWrapper);

        return ResponseResult.okResult(roleList);
    }
}

